#使用opencv对图像的边缘进行膨胀
import cv2
import numpy as np
import os


# 使用opencv对图像的边缘进行膨胀
import cv2
import numpy as np

# 读取图像
img = cv2.imread(r"mask/contour_.png")

# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 使用Canny算法检测边缘
edges = cv2.Canny(gray, 50, 150)

# 定义一个5x5的结构元素
kernel = np.ones((5,5),np.uint8)

# 使用dilate函数进行膨胀
dilated = cv2.dilate(edges, kernel)

for x in range(0, dilated.shape[0]):
    for y in range(0, dilated.shape[1]):
        if dilated[x][y] == 255:
            dilated[x][y] = 0
        else:
            dilated[x][y] = 255

# 降低图像的分辨率
# dilated = cv2.resize(dilated, (int(dilated.shape[1] / 4), int(dilated.shape[0] / 4)))

cv2.imwrite(r"contour/4_dilated.png", dilated)

cv2.imshow('Dilated Edges', dilated)
cv2.waitKey(0)
